//
//  VRDiscoverTopView.swift
//  VoiceRoom
//
//  Created by Rui on 2024/3/14.
//

import UIKit

class VRDiscoverTopView: UIView {

    private let recommendButton         = c_make_ex(UIButton.self)
        .text("Recommend").color("#FFFC00",state: .selected).font(18.font_bold)
        .color(.white.alpha(0.9)).end()
    private let followingButton         = c_make_ex(UIButton.self)
        .text("Following").color("#FFFC00",state: .selected).font(18.font_bold)
        .color(.white.alpha(0.9)).end()
    private let recommendImg         = c_make_ex(UIImageView.self)
        .image("Discover_check").end()
    private let followingImg         = c_make_ex(UIImageView.self)
        .image("Discover_check").end()
    private let postButton         = c_make_ex(UIButton.self)
        .image("Discover_post").end()
    
    var recommendBlock: (() -> Void)?
    var followingBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            recommendButton
            followingButton
            recommendImg
            followingImg
            postButton
        }
        
        postButton.trailing(15).width(75).height(30).top(7)
        recommendButton.leading(0).width(145).height(23)
        recommendButton.CenterY == postButton.CenterY
        recommendImg.width(20).height(7)
        recommendImg.CenterX == recommendButton.CenterX
        recommendImg.Top == recommendButton.Bottom + 2
        followingButton.leading(145).width(105).height(23)
        followingButton.CenterY == postButton.CenterY
        followingImg.width(20).height(7)
        followingImg.CenterX == followingButton.CenterX
        followingImg.Top == followingButton.Bottom + 2
        
        recommendButton.isSelected = true
        followingButton.isSelected = false
        recommendImg.isHidden = false
        followingImg.isHidden = true
        
        postButton.addTarget(self, action: #selector(clickPostButton), for: .touchUpInside)
        recommendButton.addTarget(self, action: #selector(clickRecommendButton), for: .touchUpInside)
        followingButton.addTarget(self, action: #selector(clickFollowingButton), for: .touchUpInside)
    }
    
    @objc private func clickPostButton(){
        
        currentViewController().present(VRPushStoryViewController(), animated: true)
    }
    
    @objc private func clickRecommendButton(){
        
        if !recommendButton.isSelected {
            
            recommendButton.isSelected = true
            followingButton.isSelected = false
            recommendImg.isHidden = false
            followingImg.isHidden = true
            recommendBlock?()
        }
    }
    
    @objc private func clickFollowingButton(){
        
        if !followingButton.isSelected {
            
            recommendButton.isSelected = false
            followingButton.isSelected = true
            recommendImg.isHidden = true
            followingImg.isHidden = false
            followingBlock?()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}
